//
//  SleepMoreController.m
//  SleepingElf
//
//  Created by karong on 2023/10/24.
//

#import "SleepMoreController.h"

@interface SleepMoreController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainViewBottom;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIPickerView *leftPicker;
@property (weak, nonatomic) IBOutlet UIPickerView *rightPicker;
/** 选择的次数 */
@property (nonatomic, assign)NSInteger selectTime;
/** 选择的分 */
@property (nonatomic, assign)NSInteger selectMin;

@end

@implementation SleepMoreController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectTime = 0;
    self.selectMin = 0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.leftPicker.subviews[1].backgroundColor = UIColor.clearColor;
        self.rightPicker.subviews[1].backgroundColor = UIColor.clearColor;
    });
}

- (IBAction)saveAction:(id)sender {
    appConfig.sleepConfig.playAgainMint = self.selectMin;
    appConfig.sleepConfig.playAgainTime = self.selectTime;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)closeAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == self.leftPicker) {
        return 16;
    }
    else {
        return 6;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 40;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 50;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UIColor * color = [UIColor colorFromHexRGB:@"ffffff" alpha:0.8];
    NSString * text = SESF(@"%02ld", (long)row);
    if (pickerView == self.leftPicker) {
        color = (row == self.selectMin)?[UIColor colorFromHexRGB:@"ffffff"]:[UIColor colorFromHexRGB:@"ffffff" alpha:0.8];
    }
    else if (pickerView == self.rightPicker) {
        color = (row == self.selectTime)?[UIColor colorFromHexRGB:@"ffffff"]:[UIColor colorFromHexRGB:@"ffffff" alpha:0.8];
    }
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:30];
    label.textColor = color;
    label.text = text;
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (pickerView == self.leftPicker) {
        self.selectMin = row;
    }
    else if (pickerView == self.rightPicker) {
        self.selectTime = row;
    }
    [pickerView reloadComponent:component];
}

@end
